#ifndef LENSTRAYMAGAZINE_H
#define LENSTRAYMAGAZINE_H

#include "AxisModule/singleaxis.h"
#include "CompositeElement/sccylinder.h"
#include "indexvalidator.h"
#include "lenstraymoduleconfig.h"
#include "lenstraymodulestate.h"
#include "namedefinition.h"
#include "systemCore/worker.h"

class LensTrayMagazine : public Worker
{
    Q_OBJECT

    enum MagPosType
    {
        KickTrayPos,
        RetrieveTrayPos
    };

public:
    explicit LensTrayMagazine(LensTrayModuleConfig *ltConfig, LensTrayModuleState *ltState, QObject *parent = nullptr);

    // Worker interface

protected:
    virtual void initEventHandle() override;
    virtual void startEventHandle(const WorkerEvent &event) override;
    virtual void stopEventHandle() override {}
    virtual void allWorkerStopped() override;
    virtual void purgeEventHandle() override;
    virtual void resetEventHandle() override;
    virtual void homeWorker(bool moveToReadyPosAfterHome) override;

    // 自动运行逻辑
public slots:
    void moveToNextRawTrayPos(bool stayThere = false);
    void moveToFirstInUseTrayPos(bool stayThere = false);
    void kickTray();
    void retractKicker();
    void setCurrentTrayState(MagTrayState::State state);
    void changeMagazine();
    void setIsLoadingUnloadingTray(bool value);
    void bhStopped();

    // 手动调试操作
public slots:
    void reqMoveToKickTrayPos(int trayIndex);
    void reqMoveToRetrieveTrayPos(int trayIndex);
    void setInLoadUnloadTrayLoopTestMode(bool value);
    void resetMagTrayState();

private:
    void getValidUIIndex(int &trayIndex);
    void moveToKickTrayPos(int trayIndex);
    void moveToRetrieveTrayPos(int trayIndex);
    void handleStop();

private:
    const bool ScanFromUpToDown = false;

    int currentMagTrayPosType = -1;
    int currentTrayIndex = -1;

    LensTrayModuleConfig *ltConfig;
    LensTrayModuleState *ltState;

    SingleAxis *ltMagZ;
    SCCylinder *ltKicker;
    SCDI *ltMagTraySensor;
    SCDI *ltMagSensor;

    bool inLoadUnloadTrayLoopTestMode = false;
    bool m_isLoadingUnloadingTray = false;
    bool isBhStopped = false;
};

#endif    // LENSTRAYMAGAZINE_H
